package io.netty.example.mytest.nio.reactor;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @ClassName AcceptEventHandler
 * @Description accept事件句柄
 * @Author dukezeng
 * @CreatedTime 2019/4/13 18:08
 * @Version 1.0
 **/
public class AcceptEventHandler implements EventHandler {
    private Selector demultiplexer;
    public AcceptEventHandler(Selector demultiplexer) {
        this.demultiplexer = demultiplexer;
    }

    public void handleEvent(SelectionKey accpetHandle) throws Exception {
        System.out.println("===== Accept Event Handler =====");
        ServerSocketChannel serverSocketChannel = (ServerSocketChannel) accpetHandle.channel();
        //接收客户端channel
        SocketChannel socketChannel = serverSocketChannel.accept();
        if (socketChannel != null) {
            socketChannel.configureBlocking(false);
            socketChannel.register(demultiplexer, SelectionKey.OP_READ);
        }

    }
}
